<template>
  <el-dialog class="lzxczxrfetrxer" v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false"
    :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()"
      label-width="120px">
      <el-form-item label="员工" prop="userId">
        <el-select class="w100" disabled @change="jxcrtxr" v-model="dataForm.userId" placeholder="请选择员工" clearable filterable>
          <el-option v-for="item in xczxtrt"
           :key="item.id" :label="item.realName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-row :gutter="0">
        <el-col :span="4">
          <el-form-item label="月份" >
            <el-input   v-model="dataForm.month"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="岗位/工号" >
            <el-input   v-model="dataForm.companyNo"></el-input>
          </el-form-item>
        </el-col>
 
        <el-col :span="4">
          <el-form-item label="入职日期" >
            <el-input   v-model="dataForm.companyTime"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="4">
          <el-form-item label="扣餐费" >
            <el-input   v-model="dataForm.mealUp"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="住宿水电" >
            <el-input   v-model="dataForm.hydropower"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="扣迟到缺卡" >
            <el-input   v-model="dataForm.later"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="转账费" >
            <el-input   v-model="dataForm.transfer"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="税费3% " >
            <el-input   v-model="dataForm.taxation"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="其它扣款" >
            <el-input   v-model="dataForm.otherTake"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="其它补贴" >
            <el-input   v-model="dataForm.otherSubsidy"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="保险" >
            <el-input   v-model="dataForm.insure"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="借资" >
            <el-input   v-model="dataForm.borrow"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="总扣款" >
            <el-input   v-model="dataForm.total"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="应发工资" >
            <el-input   v-model="dataForm.balance"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="实际工资" >
            <el-input   v-model="dataForm.wages"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="已发工资/备注 " >
            <el-input  type="textarea" rows="5"  v-model="dataForm.remark"></el-input>
          </el-form-item>
        </el-col>
      </el-row>


      <!-- <el-form-item label="照片" prop="photo">
        <el-input v-model="dataForm.photo" placeholder="照片"></el-input>
      </el-form-item>
          <el-form-item label="年" prop="year">
        <el-input v-model="dataForm.year" placeholder="年"></el-input>
      </el-form-item>
          <el-form-item label="月" prop="month">
        <el-input v-model="dataForm.month" placeholder="月"></el-input>
      </el-form-item>
          <el-form-item label="日" prop="day">
        <el-input v-model="dataForm.day" placeholder="日"></el-input>
      </el-form-item>
          <el-form-item label="小时" prop="hour">
        <el-input v-model="dataForm.hour" placeholder="小时"></el-input>
      </el-form-item>
          <el-form-item label="创建时间" prop="createTime">
        <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
      </el-form-item>
          <el-form-item label="更新时间" prop="updateTime">
        <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
      </el-form-item> -->
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { ElMessage } from "element-plus";
import { dxget } from "@/utils/utils";
const emit = defineEmits(["refreshDataList"]);
const xczxtrt = ref([])
const visible = ref(false);
const dataFormRef = ref();

const jkjmxzcf = ref({
  kdfdsf1:"",
  kdfdsf2:"",
  kdfdsf3:"",
  kdfdsf4:"",
  kdfdsf5:"",
  kdfdsf6:"",
  kdfdsf7:"",
  kdfdsf8:"",
  kdfdsf9:"",
  kdfdsf10:"",
  kdfdsf11:"",
  kdfdsf12:"",
  kdfdsf13:"",
  kdfdsf14:"",
  kdfdsf15:"",
  kdfdsf16:"",
  kdfdsf17:"",
  kdfdsf18:""
})


const dataForm = reactive({
  id: '', companyId: '', content: '', userId: '', photo: '', year: '', month: '', day: '', hour: '', createTime: '', updateTime: ''
});
const kjmxzczfd= ()=>{
  const kfdsf =  jkjmxzcf.value.kdfdsf6 +
  jkjmxzcf.value.kdfdsf7 +
  jkjmxzcf.value.kdfdsf8 +
  jkjmxzcf.value.kdfdsf9 +
  jkjmxzcf.value.kdfdsf10 +
  jkjmxzcf.value.kdfdsf11 +
  jkjmxzcf.value.kdfdsf12 +
  jkjmxzcf.value.kdfdsf13

  jkjmxzcf.value.kdfdsf14 = kfdsf
  jkjmxzcf.value.kdfdsf16 = (jkjmxzcf.value.kdfdsf15 - kfdsf)
  return kfdsf
}

const rules = ref({
  companyId: [
    { required: true, message: '必填项不能为空', trigger: 'blur' }
  ],
  content: [
    { required: true, message: '必填项不能为空', trigger: 'blur' }
  ],
  userId: [
    { required: true, message: '必填项不能为空', trigger: 'blur' }
  ],
  photo: [
    { required: true, message: '必填项不能为空', trigger: 'blur' }
  ],
  year: [
    { required: true, message: '必填项不能为空', trigger: 'blur' }
  ],
  month: [
    { required: true, message: '必填项不能为空', trigger: 'blur' }
  ],
  day: [
    { required: true, message: '必填项不能为空', trigger: 'blur' }
  ],
  hour: [
    { required: true, message: '必填项不能为空', trigger: 'blur' }
  ],
  createTime: [
    { required: true, message: '必填项不能为空', trigger: 'blur' }
  ],
  updateTime: [
    { required: true, message: '必填项不能为空', trigger: 'blur' }
  ]
});

const init = (id?: number) => {
  visible.value = true;
  dataForm.id = "";
  jkjmxzcf.value = {
  kdfdsf1:"",
  kdfdsf2:"",
  kdfdsf3:"",
  kdfdsf4:"",
  kdfdsf5:"",
  kdfdsf6:"",
  kdfdsf7:"",
  kdfdsf8:"",
  kdfdsf9:"",
  kdfdsf10:"",
  kdfdsf11:"",
  kdfdsf12:"",
  kdfdsf13:"",
  kdfdsf14:"",
  kdfdsf15:"",
  kdfdsf16:"",
  kdfdsf17:"",
  kdfdsf18:"",
}

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (id) {
    getInfo(id);
  }
};

// 获取信息
const getInfo = (id: number) => {
  baseService.get("/api/salary/" + id).then((res) => {
    Object.assign(dataForm, res.data);
    jkjmxzcf.value = JSON.parse(dataForm.content)
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    // dataForm.content = JSON.stringify(jkjmxzcf.value)
    // dataForm.balance = jkjmxzcf.value.kdfdsf16
    // dataForm.jobDay = jkjmxzcf.value.kdfdsf4
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/api/salary", dataForm).then((res) => {
      ElMessage.success({
        message: '成功',
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
const jmkxcf = async () => {

  const { list } = await dxget('/api/user/page', {
    limit: 999,
    isAuthentication:true
  })
  xczxtrt.value = list
}
jmkxcf()
const jxcrtxr = (data) => {
  const obj = xczxtrt.value.filter(a => a.id == data)[0]
  jkjmxzcf.value.kdfdsf1 = obj.companyNo
  jkjmxzcf.value.kdfdsf18= obj.realName

}
</script>
